package com.tang.crm.controller;

import com.tang.crm.base.BaseController;
import com.tang.crm.base.ResultInfo;
import com.tang.crm.dto.CustomerServeDto;
import com.tang.crm.entity.CustomerServe;
import com.tang.crm.service.CustomerServeService;
import com.tang.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 服务管理
 * @author SHIRELY阳
 */
@Controller
@RequestMapping("/customer_serve")
public class CustomerServeController extends BaseController {

    @Autowired
    private CustomerServeService customerServeService;

    /**
     * 通过不同的类型进入不同的服务页面
     * @param type
     * @return
     */
    @RequestMapping("/index/{type}")
    public String index(@PathVariable Integer type){
        // 判断类型是否为空
        if (type != null) {
            if (type == 1) {
                // 服务创建
                return "customerServe/customer_serve";
            } else if (type == 2) {
                // 服务分配
                return "customerServe/customer_serve_assign";
            } else if (type == 3) {
                // 服务处理
                return "customerServe/customer_serve_proce";
            } else if (type == 4) {
                // 服务反馈
                return "customerServe/customer_serve_feed_back";
            } else if (type == 5) {
                // 服务归档
                return "customerServe/customer_serve_archive";
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 打开创建服务页面
     * @return
     */
    @RequestMapping("/toAddCustomerServePage")
    public String toAddCustomerServePage(){
        return "customerServe/customer_serve_add";
    }

    /**
     * 打开服务分配页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toCustomerServeAssignPage")
    public String toCustomerServeAssignPage(Integer id, Model model) {
        model.addAttribute("customerServe", customerServeService.getById(id));
        return "customerServe/customer_serve_assign_add";
    }

    /**
     * 打开服务处理页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toCustomerServeProcePage")
    public String toCustomerServeProcePage(Integer id, Model model) {
        model.addAttribute("customerServe", customerServeService.getById(id));
        return "customerServe/customer_serve_proce_add";
    }

    /**
     * 打开服务反馈页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toCustomerServeFeedBackPage")
    public String toCustomerServeFeedBackPage(Integer id, Model model) {
        model.addAttribute("customerServe", customerServeService.getById(id));
        return "customerServe/customer_serve_feed_back_add";
    }

    /**
     * 分页查询
     * @param customerServeDto
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> selectByParams(CustomerServeDto customerServeDto, Integer flag,
                                              HttpServletRequest request) {
        // 判断是否执行服务处理，如果是则查询分配给当前登录用户的服务记录
        if (flag != null && flag == 1) {
            // 设置查询条件：分配人
            customerServeDto.setAssigner(LoginUserUtil.releaseUserIdFromCookie(request));
        }
        return customerServeService.selectByParams(customerServeDto);
    }

    /**
     * 添加服务
     * @param customerServe
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addCustomerServe(CustomerServe customerServe) {
        customerServeService.addCustomerServe(customerServe);
        return success("添加服务成功");
    }

    /**
     * 更新服务
     *   1.服务分配
     *   2.服务处理
     *   3.服务反馈
     * @param customerServe
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateCustomerServe(CustomerServe customerServe) {
        customerServeService.updateCustomerServeState(customerServe);
        return success("更新服务成功");
    }
}
